capture log close
clear all
set more off 

********************************************************************************
// Simulation of covariate marginal effects
********************************************************************************

use "PCSALevelData_v3.dta", clear
cap drop og*

capture program drop brentry_bioprobit_neg
qui do "brentry_bioprobit_negbin_v3.do"
constraint drop
constraint 1 tot_pop = 1
constraint 2 tot_pop2 = 1
ml clear
ml model lf brentry_bioprobit_neg (hosp_s:cat_hosp2 = tot_pop, nocons) (hosp_v:cat_hosp2 = rural income_pc hispanic nonhisp_black gte_highschool age_65 uninsured, nocons) (hosp_f:cat_hosp2 = cms_wage_index con_intensity, nocons) /hosp_a1 /hosp_g1 (ucc_s:cat_ucc = tot_pop2, nocons) (ucc_v:cat_ucc = n_hospitals rural income_pc hispanic nonhisp_black gte_highschool age_65 uninsured, nocons) (ucc_f:cat_ucc = cms_wage_index, nocons) /ucc_a1 /ucc_a2 /ucc_a3 /ucc_g1 /ucc_g2 /ucc_g3 /r, constraints(1 2) technique(bfgs)
eststo: ml max, difficult iterate(200) tolerance(1e-4) ltolerance(1e-5)
estimates save "bioprobit_ucc.ster", replace

estimates use "bioprobit_ucc.ster"

// Simulation

* Predict number of UCCs at observed X

* Endogenous number of hospitals
*ren cat_hosp2 og_cat_hosp2
*gen hosp_v = _b[hosp_v:rural]*rural+_b[hosp_v:income_pc]*income_pc+_b[hosp_v:hispanic]*hispanic+_b[hosp_v:nonhisp_black]*nonhisp_black+_b[hosp_v:gte_highschool]*gte_highschool+_b[hosp_v:age_65]*age_65+_b[hosp_v:uninsured]*uninsured
*gen hosp_f = _b[hosp_f:con_intensity]*con_intensity+_b[hosp_f:cms_wage_index]*cms_wage_index
*gen hosp_a1 = /hosp_a1 
*gen hosp_g1 = /hosp_g1
*gen pi_hosp = tot_pop*(hosp_v + hosp_a1) - hosp_f - hosp_g1
*gen cat_hosp2 = 0
*replace cat_hosp2 = 1 if pi_hosp>0
*ren n_hospitals og_n_hospitals 
*gen n_hospitals = cat_hosp2

* Fixed number of hospitals
gen ucc_v = _b[ucc_v:n_hospitals]*n_hospitals+_b[ucc_v:rural]*rural+_b[ucc_v:income_pc]*income_pc+_b[ucc_v:hispanic]*hispanic+_b[ucc_v:nonhisp_black]*nonhisp_black+_b[ucc_v:gte_highschool]*gte_highschool+_b[ucc_v:age_65]*age_65+_b[ucc_v:uninsured]*uninsured
gen ucc_f = _b[ucc_f:cms_wage_index]*cms_wage_index
gen ucc_a1 = /ucc_a1
gen ucc_a2 = /ucc_a2
gen ucc_a3 = /ucc_a3
gen ucc_g1 = /ucc_g1
gen ucc_g2 = /ucc_g2
gen ucc_g3 = /ucc_g3
gen pi_ucc_1 = tot_pop*(ucc_v + ucc_a1) - ucc_f - ucc_g1
gen pi_ucc_2 = tot_pop*(ucc_v + ucc_a1 - ucc_a2) - ucc_f - ucc_g1 - ucc_g2
gen pi_ucc_3 = tot_pop*(ucc_v + ucc_a1 - ucc_a2 - ucc_a3) - ucc_f - ucc_g1 - ucc_g2 - ucc_g3
gen hat_ucc = .
replace hat_ucc = 0 if pi_ucc_1<0
replace hat_ucc = 1 if pi_ucc_2<0 & pi_ucc_1>0
replace hat_ucc = 2 if pi_ucc_3<0 & pi_ucc_2>0
replace hat_ucc = 3 if pi_ucc_3>0
*drop cat_hosp2
*ren og_cat_hosp2 cat_hosp2
*drop n_hospitals 
*ren og_n_hospitals n_hospitals

* Predict number of UCCs at cat_hosp2=0
gen ucc_v2 = _b[ucc_v:n_hospitals]*0+_b[ucc_v:rural]*rural+_b[ucc_v:income_pc]*income_pc+_b[ucc_v:hispanic]*hispanic+_b[ucc_v:nonhisp_black]*nonhisp_black+_b[ucc_v:gte_highschool]*gte_highschool+_b[ucc_v:age_65]*age_65+_b[ucc_v:uninsured]*uninsured
gen pi_ucc_1_b2 = tot_pop*(ucc_v2 + ucc_a1) - ucc_f - ucc_g1
gen pi_ucc_2_b2 = tot_pop*(ucc_v2 + ucc_a1 - ucc_a2) - ucc_f - ucc_g1 - ucc_g2
gen pi_ucc_3_b2 = tot_pop*(ucc_v2 + ucc_a1 - ucc_a2 - ucc_a3) - ucc_f - ucc_g1 - ucc_g2 - ucc_g3
gen hat_ucc2 = .
replace hat_ucc2 = 0 if pi_ucc_1_b2<0
replace hat_ucc2 = 1 if pi_ucc_2_b2<0 & pi_ucc_1_b2>0
replace hat_ucc2 = 2 if pi_ucc_3_b2<0 & pi_ucc_2_b2>0
replace hat_ucc2 = 3 if pi_ucc_3_b2>0

* Predict number of UCCs at counterfactual X

ren cat_hosp2 og_cat_hosp2
gen cat_hosp2 = 1
ren n_hospitals og_n_hospitals 
gen n_hospitals = cat_hosp2
predict ucc_v_cf, xb equation(ucc_v)
predict ucc_f_cf, xb equation(ucc_f)
gen pi_ucc_1_cf = tot_pop*(ucc_v_cf + ucc_a1) - ucc_f_cf - ucc_g1
gen pi_ucc_2_cf = tot_pop*(ucc_v_cf + ucc_a1 - ucc_a2) - ucc_f_cf - ucc_g1 - ucc_g2
gen pi_ucc_3_cf = tot_pop*(ucc_v_cf + ucc_a1 - ucc_a2 - ucc_a3) - ucc_f_cf - ucc_g1 - ucc_g2 - ucc_g3
gen hat_ucc_hosp = .
replace hat_ucc_hosp = 0 if pi_ucc_1_cf<0
replace hat_ucc_hosp = 1 if pi_ucc_2_cf<0 & pi_ucc_1_cf>0
replace hat_ucc_hosp = 2 if pi_ucc_3_cf<0 & pi_ucc_2_cf>0
replace hat_ucc_hosp = 3 if pi_ucc_3_cf>0
drop pi_ucc_1_cf pi_ucc_2_cf pi_ucc_3_cf ucc_v_cf ucc_f_cf cat_hosp2 n_hospitals
ren og_cat_hosp2 cat_hosp2 
gen n_hospitals = cat_hosp2

sum rural uninsured income_pc hispanic nonhisp_black gte_highschool age_65 con_intensity cms_wage_index

local vars "rural uninsured income_pc hispanic nonhisp_black gte_highschool age_65 con_intensity cms_wage_index"
foreach x of local vars{
	ren `x' og_`x'
	sum og_`x'
	gen `x' = og_`x' + `r(sd)'
	gen ucc_v_cf = _b[ucc_v:n_hospitals]*n_hospitals+_b[ucc_v:rural]*rural+_b[ucc_v:income_pc]*income_pc+_b[ucc_v:hispanic]*hispanic+_b[ucc_v:nonhisp_black]*nonhisp_black+_b[ucc_v:gte_highschool]*gte_highschool+_b[ucc_v:age_65]*age_65+_b[ucc_v:uninsured]*uninsured
	gen ucc_f_cf = _b[ucc_f:cms_wage_index]*cms_wage_index
	gen pi_ucc_1_cf = tot_pop*(ucc_v_cf + ucc_a1) - ucc_f_cf - ucc_g1
	gen pi_ucc_2_cf = tot_pop*(ucc_v_cf + ucc_a1 - ucc_a2) - ucc_f_cf - ucc_g1 - ucc_g2
	gen pi_ucc_3_cf = tot_pop*(ucc_v_cf + ucc_a1 - ucc_a2 - ucc_a3) - ucc_f_cf - ucc_g1 - ucc_g2 - ucc_g3
	gen hat_ucc_`x' = .
	replace hat_ucc_`x' = 0 if pi_ucc_1_cf<0
	replace hat_ucc_`x' = 1 if pi_ucc_2_cf<0 & pi_ucc_1_cf>0
	replace hat_ucc_`x' = 2 if pi_ucc_3_cf<0 & pi_ucc_2_cf>0
	replace hat_ucc_`x' = 3 if pi_ucc_3_cf>0
	drop pi_ucc_1_cf pi_ucc_2_cf pi_ucc_3_cf ucc_v_cf ucc_f_cf `x' 
	*hosp_v_cf hosp_f_cf pi_hosp_cf cat_hosp2 n_hospitals
	ren og_`x' `x'
}

keep cat_ucc hat_ucc* tot_pop n_urgentcare
local vars "hosp uninsured rural income_pc hispanic nonhisp_black gte_highschool age_65 con_intensity cms_wage_index"
*drop hat_ucc
*ren n_urgentcare hat_ucc
foreach x of local vars{
	gen diff_`x' = hat_ucc_`x' - hat_ucc
}
drop diff_hosp
gen diff_hosp = hat_ucc_hosp - hat_ucc2
sum diff*
save "MeanSimulations.dta", replace

* CON laws for hospitals

use "PCSALevelData_v3.dta", clear
cap drop og*
estimates use "bioprobit_ucc.ster"

* Predict number of hospitals at observed X

ren cat_hosp2 og_cat_hosp2
predict hosp_v, xb equation(hosp_v)
predict hosp_f, xb equation(hosp_f)
gen hosp_a1 = /hosp_a1 
gen hosp_g1 = /hosp_g1
gen pi_hosp = tot_pop*(hosp_v + hosp_a1) - hosp_f - hosp_g1
gen cat_hosp2 = 0
replace cat_hosp2 = 1 if pi_hosp>0

* Predict number of hospitals at counterfactual X

local vars "rural uninsured income_pc hispanic nonhisp_black gte_highschool age_65 con_intensity cms_wage_index"
foreach x of local vars{
	ren `x' og_`x'
	sum og_`x'
	gen `x' = og_`x' + `r(sd)'
	predict hosp_v_cf, xb equation(hosp_v)
	predict hosp_f_cf, xb equation(hosp_f)
	gen pi_hosp_cf = tot_pop*(hosp_v_cf + hosp_a1) - hosp_f_cf - hosp_g1
	gen cat_hosp2_`x' = 0
	replace cat_hosp2_`x' = 1 if pi_hosp_cf>0
	drop pi_hosp_cf hosp_v_cf hosp_f_cf `x'
	ren og_`x' `x'
}

keep cat_hosp2* tot_pop
local vars "rural uninsured income_pc hispanic nonhisp_black gte_highschool age_65 con_intensity cms_wage_index"
foreach x of local vars{
	gen diff_`x' = cat_hosp2_`x' - cat_hosp2
}
sum diff_*
save "MeanSimulationsHosp.dta", replace